
<template>
<el-container class="home-container">
    <!-- 头部区域 -->
  <el-header>
      <div>
          <img id="img1" src="../assets/logo1.jpg" alt="头像">
          <span>西农电商后台管理</span>
      </div>
       <el-button type="info" @click="logOut">退出</el-button>
  </el-header>
  <!-- 页面主体区 -->
  <el-container>
      <!-- 侧边栏 区域-->
    <el-aside :width="isCollapse ?'64px':'200px'">
      <div class="toggle-button" @click="toggle" title="折叠侧边栏">|||</div>
        <!-- 侧边栏菜单区域 -->
    <el-menu background-color="#333744" text-color="#fff" active-text-color="#409Eff" :unique-opened="true" :collapse="isCollapse"
    :collapse-transition="false" :router="true" :default-active="myactivePath"><!-- 开启路由模式 -->
      <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id"><!-- 一级菜单 -->
        <template slot="title"><!-- 一级菜单模板区域 -->
          <i :class="iconsObj[item.id]"></i><!-- 图标 -->
          <span>{{item.authName}}</span><!-- 文本 -->
        </template>
        <!-- 二级菜单 -->
        <el-menu-item  :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
        @click="saveState('/'+subItem.path)">
        <template slot="title"><!-- 二级菜单模板区域 -->
           <i class="el-icon-menu"></i><!-- 图标 -->
           <span>{{subItem.authName}}</span><!-- 文本 -->
        </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <!-- 右侧内容区域 -->
    <el-main>
      <!-- 路由展占位符 -->
      <router-view></router-view>
      </el-main>
  </el-container>
</el-container>
</template>

<script>

export default {
  data () {
    return {
      /* 左侧菜单数据 */
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      /* 是否折叠 */
      isCollapse: false,
      myactivePath: ''/* 激活链接地址 */
    }
  },
  created () {
    this.getMenuList();
    this.myactivePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    logOut: function () {
      window.sessionStorage.clear();
      this.$router.push('/login');
    },
    /* 获取所有菜单 */
    async getMenuList () {
      const { data: res } = await this.$http.get('menus');/* 获取来自menu接口的数据 */
      if (res.meta.status !== 200) { return this.$message.error(res.meta.msg); }
      this.menulist = res.data;
      // console.log(res);
    },
    /* 菜单折叠与展开 */
    toggle: function () {
      this.isCollapse = !this.isCollapse;
    },
    /* 保存链接的激活状态 */
    saveState: function (activePath) {
      window.sessionStorage.setItem('activePath', activePath);
      this.myactivePath = activePath;
    }
  }

}
</script>
<style lang="less" scoped>
.el-header{
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    display: flex;
    justify-content: space-between;
    background-color: #373d41;
    div{
        display: flex;
        align-items: center;
        span{
            padding-left: 15px;
        }
    }
}
.el-aside{
    background-color: #333744;
    .el-menu{
      border-right: 0;
    }
}
.el-main{
    background-color: #eaedf1;
}
.home-container{
    height: 100%;
}
#img1{
    width: 60px;
    height: 60px;
    border: 1px solid;
    border-radius: 2px;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: #4a5064;
  color: #fff;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  letter-spacing: .2em;
  cursor: pointer;
}
</style>
